<!--  -->
<template>
  <div class="search-result">
    <van-list
    v-model:loading="loading"
    :finished="finished"
    finished-text="没有更多了"
    v-model:error="error"
    error-text="请求失败，点击重新加载"
    @load="onLoad"
    >
    <!-- <van-cell v-for="(article,index) in list" :key="index" :title="article.title" /> -->
    <article-item v-for="(item,index) in list" :key="index"
                  :articles='item'
    ></article-item>
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from '../../../api/search.js'
import articleItem from '@/views/home/components/articleItem.vue'
export default {
  components: {
    articleItem
  },
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      error: false,
      page: 1,
      per_page: 20
    }
  },
  methods: {
    async onLoad () {
      // 请求后台数据
      try {
        const res = await getSearchResult({
          page: this.page, // 页码
          per_page: this.per_page, // 每页数据条数
          q: this.searchText // 搜索关键词
        })
        // 将数据添加到列表，注意不能直接赋值
        this.list.push(...res.data.data.results)
        // console.log('结果', this.list)
        // 判断是否还有数据
        this.loading = false // 每次上滑自动设置为true，加载完设置为false不,然一直显示加载动画
        if (res.data.data.results) {
          this.page++ // 有 则加载下一页
        } else {
          // 这里表示数据全部加载完毕
          this.finished = true
          this.loading = false
        }
      } catch (error) {
        // 加载失败，显示失败提示动画
        this.error = true
        this.loading = false
      }
    }
  }
}
</script>

<style  scoped lang='less'>
.van-list {
  height: 91.5vh;
  overflow-y: auto;
}
</style>
